package com.ys.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 行业转化词
 * @author xiezhengliang
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/industryConversionwordController")
public class IndustryConversionwordController {

	/**
	 * 跳到百度数据的行业转化词页面
	 * @param request
	 * @return
	 */
	@RequestMapping(params="industryConversionwordBaidu")
	public ModelAndView industryConversionwordBaidu(HttpServletRequest request){
		return new ModelAndView("industryConversionword/industryConversionwordBaidu");
	}
	
	/**
	 * 跳到易数宝数据的行业转化词页面
	 * @param request
	 * @return
	 */
	@RequestMapping(params="industryConversionwordYsb")
	public ModelAndView industryConversionwordYsb(HttpServletRequest request){
		return new ModelAndView("industryConversionword/industryConversionwordYsb");
	}
	
	
	@RequestMapping(params="getIndustryStandard")
	public void  getRemotetIndustryStandard(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
			try {
				CloseableHttpClient httpclient = HttpClients.createDefault(); 
				URI uri = new  URIBuilder()
						.setScheme("http")  
						.setHost("127.0.0.1")
						.setPort(8080)
//				        .setHost("localhost")
//				        .setPort(21680)
				        .setPath("/yishengAnalyze/yishengIndustryStandardController.do")
				        .setParameter("getIndustryStandard", null)
				        .build();
				
				HttpGet httpGet = new HttpGet(uri);
				System.out.println(httpGet.getURI());
				CloseableHttpResponse response = httpclient.execute(httpGet);
				 StatusLine statusLine = response.getStatusLine();  
			     HttpEntity entity = response.getEntity();  
				if (statusLine.getStatusCode() >= 300) {  
		            throw new HttpResponseException(  
		                    statusLine.getStatusCode(),  
		                    statusLine.getReasonPhrase());  
		        }  
		        if (entity == null) {  
		            throw new ClientProtocolException("Response contains no content");  
		        }
		        
		        ContentType contentType = ContentType.getOrDefault(entity); 
		        Charset charset = contentType.getCharset();  
		        String content= IOUtils.toString(entity.getContent(),"utf-8"); 
		        httpServletResponse.setContentType("text/html");
		        httpServletResponse.setCharacterEncoding("utf-8");
		        httpServletResponse.setHeader("Cache-Control", "no-store");
		        PrintWriter printWriter = httpServletResponse.getWriter();
		        printWriter.println(content);
			} catch (URISyntaxException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}
